/*
 * Copyright 2010 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yannickstucki.android.musicqueue.ui;

import java.util.LinkedList;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

import com.yannickstucki.android.musicqueue.R;
import com.yannickstucki.android.musicqueue.communication.ISongCommander;
import com.yannickstucki.android.musicqueue.communication.OnDragListener;
import com.yannickstucki.android.musicqueue.communication.OnUiUpdateListener;
import com.yannickstucki.android.musicqueue.data.Song;

/**
 * The main screen mode. It shows which songs is being played and displays a few
 * of the next songs. The user can drag the songs around or remove them. There
 * are also play/pause controls.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class PlayingScreenMode implements IScreenMode, OnUiUpdateListener, OnDragListener<Song> {

  /**
   * Through this reference is the music play back updated.
   */
  private ISongCommander songCommander;

  /**
   * The central view where the user can drag and swipe the songs.
   */
  private DragView<Song> dragView;

  /**
   * A button for toggling play/pause.
   */
  private ImageButton playPauseButton;

  /**
   * A button for displayin the queue count.
   * 
   * TODO: add action on click.
   */
  private Button queueButton;

  /**
   * The list of songs in the queue.
   */
  private LinkedList<Song> queue;

  /**
   * The constructor needs a reference to ISongCommander.
   */
  public PlayingScreenMode(ISongCommander songCommander) {
    this.songCommander = songCommander;
  }

  /**
   * All the UI elements are passed in from the outside.
   */
  public void setUiElements(DragView<Song> dragView, ImageButton playPauseButton,
      ImageButton shuffleButton, Button queueButton) {
    
    Song.currentDisplayStringIndex = Song.DISPLAY_INDEX_PORTRAIT;   
    
    this.dragView = dragView;
    this.playPauseButton = playPauseButton;
    this.queueButton = queueButton;
    
    playPauseButton.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        PlayingScreenMode.this.songCommander.playPause();
      }

    });

    shuffleButton.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        shuffle();
      }
    });
  }

  @Override
  public OnUiUpdateListener getOnUiUpdateListener() {
    return this;
  }

  @Override
  public void pause() {
    playPauseButton.setImageResource(R.drawable.ic_media_play);
  }

  @Override
  public void play() {
    playPauseButton.setImageResource(R.drawable.ic_media_pause);
  }

  @Override
  public void setDisplayedSongs(Song[] displayedSongs) {
    dragView.setContent(displayedSongs);
  }
  
  @Override
  public void onDisplayedSongsChanged() {
    dragView.notifyContentChanged();
  }
  
  @Override
  public void onDisplayedSongPushed() {
    dragView.notifyElementPushed();
  }

  @Override
  public void onQueueChanged() {
    queueButton.setText(Integer.toString(queue.size()));
  }

  @Override
  public void setQueue(LinkedList<Song> queue) {
    this.queue = queue;
    onQueueChanged();
  }

  /**
   * Performs the shuffle. All songs except the top song are being replaced.
   */
  private void shuffle() {
    songCommander.shuffle();
  }

  @Override
  public void onElementRemoved() {
    songCommander.notifySongRemoved();
  }

  @Override
  public void onTopElementChanged() {
    songCommander.notifyCurrentSongChanged();
  }
}
